import pandas as pd
import datetime
import math
import os
import Core.Config as Config
import Core.Gadget as Gadget
import numpy as np


class LoadSystematicFactorFromFile(object):
    def __init__(self, path_filename, sheet_name, header_row_num=0, data_row_num=1):
        self.path_filename = path_filename
        self.sheet_name = sheet_name
        self.header_row_num = header_row_num
        self.data_row_num = data_row_num

        #
        # df_data = pd.read_excel(path_filename, sheet_name=sheet_name, index_col=0).iloc[1:, :].dropna().astype('float32')
        df_data = pd.read_excel(self.path_filename, sheet_name=self.sheet_name, header=self.header_row_num)
        self.df = df_data.iloc[self.data_row_num:, :].copy()

        target_field = self.df.columns[0]
        self.df.rename(columns={target_field:"date"}, inplace=True)
        aa = 0

    def load(self, header_name):
        df_factor = self.df[["date", header_name]].copy()
        df_factor.rename(columns={header_name: "value"}, inplace=True)
        #
        return df_factor

    def save_to_database(self, database):
        pass


if __name__ == '__main__':
    # ---Connecting Database---
    path_filename = os.getcwd() + "\..\Config\config_local.json"
    database = Config.create_database(database_type="MySQL", config_file=path_filename, config_field="MySQL")

    #
    path = str(r"C:\Users\kkwoo\Documents\Market Watch\宏观经济研究\\")
    filename = "价格-构建价格体系_v2.xlsx"
    load_file = LoadSystematicFactorFromFile(path+filename, sheet_name="同比", header_row_num=1, data_row_num=4)
    load_file.load(header_name="中国:发电量:当月值", factor_name="发电量")
    aa = 0
